package org.shuma.goods.service.impl;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.shuma.goods.domain.entity.ProductBasicInfo;
import org.shuma.goods.mapper.ProductBasicInfoDao;
import org.shuma.goods.mapper.ProductBasicInfoMapper;
import org.shuma.goods.service.ProductBasicInfoService;
import org.shuma.goods.service.ProductDetailsInfoService;
import org.shuma.goods.utils.MybatisPlusUtil;
import org.shuma.proxy.facade.Proxy;
import org.shuma.proxy.facade.commons.ProductProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class ProductBasicInfoServiceImpl extends ServiceImpl<ProductBasicInfoMapper, ProductBasicInfo>
        implements ProductBasicInfoService {
    @SofaReference(interfaceType = Proxy.class,uniqueId = "${service.proxy.unique.id}",
            binding = @SofaReferenceBinding(bindingType = "bolt"))
    private Proxy proxy;
    @Autowired
    private ProductDetailsInfoService productDetailsInfoService;
    @Autowired
    private ProductBasicInfoMapper productBasicInfoMapper;
    @Resource
    private ProductBasicInfoDao productBasicInfomapper;

    @Override
    public ProductBasicInfo getProductBasicInfo(Integer productId) {
        return productBasicInfoMapper.selectById(productId);
    }

    @Override
    public Page<ProductBasicInfo> getProductBasicInfoList(Long userId, Integer pageNum, Integer pageSize) {
        Page<ProductBasicInfo> page = new Page<>(pageNum, pageSize);
        QueryWrapper<ProductBasicInfo> queryWrapper = MybatisPlusUtil.queryWrapperBuilder(ProductBasicInfo.builder().creator(userId).build());

        this.page(page,queryWrapper);

        return page;
    }

    @Override
    public Page<ProductBasicInfo> getProductStatus(Integer productId,Integer pageNum, Integer pageSize) {
        Page<ProductBasicInfo> page = new Page<>(pageNum, pageSize);
        //QueryWrapper<ProductBasicInfo> queryWrapper = MybatisPlusUtil.queryWrapperBuilder(ProductBasicInfo.builder().productCheckState(productId).build());
        QueryWrapper<ProductBasicInfo> queryWrapper = new QueryWrapper<ProductBasicInfo>().eq("product_check_state",productId);
        this.page(page,queryWrapper);
        return page;
    }
    /**
     * 通过ID查询单条数据
     *
     * @param productId 主键
     * @return 实例对象
     */
    @Override
    public ProductBasicInfo queryById(Long productId) {
        return this.productBasicInfomapper.queryById(productId);
    }

    /**
     * 分页查询
     *
     * @param productBasicInfo 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public org.springframework.data.domain.Page<ProductBasicInfo> queryByPage(ProductBasicInfo productBasicInfo, PageRequest pageRequest) {
        long total = this.productBasicInfomapper.count(productBasicInfo);
        return new PageImpl<ProductBasicInfo>(this.productBasicInfomapper.queryAllByLimit(productBasicInfo, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param productBasicInfo 实例对象
     * @return 实例对象
     */
    @Override
    public ProductBasicInfo insert(ProductBasicInfo productBasicInfo) {
        org.shuma.goods.domain.entity.ProductBasicInfo basicInfo = this.productBasicInfomapper.insert(productBasicInfo);
        Long productId = basicInfo.getProductId();
        Long proxyId = productBasicInfo.getProxyId();
        ProductProxy productProxy = new ProductProxy();
        productProxy.setProxyId(proxyId);
        productProxy.setProductId(productId);
        proxy.addProxy(productProxy);
        return productBasicInfo;
    }

    /**
     * 修改数据
     *
     * @param productBasicInfo 实例对象
     * @return 实例对象
     */
    @Override
    public ProductBasicInfo update(ProductBasicInfo productBasicInfo) {
        ProductBasicInfo productBasicInfo1 = productBasicInfo;
        productBasicInfo1.setProductId(null);
        if (productBasicInfo.getPDetailsId() == null || productBasicInfo1==null){
            throw new RuntimeException();
        }
        this.productBasicInfomapper.update(productBasicInfo);
        return this.queryById(productBasicInfo.getProductId());
    }

    /**
     * 通过主键删除数据

     * @return 是否成功
     */
    @Override
    public boolean deleteById(List<Long> idList) {
        int count = 0;
        for (Long productId : idList) {
            org.shuma.goods.domain.entity.ProductBasicInfo productBasicInfo = productBasicInfomapper.queryById(productId);
            productDetailsInfoService.deleteById(productBasicInfo.getPDetailsId());
            productBasicInfomapper.deleteById(productId);
            proxy.deleteProxy(productId);
            count++;
        }
        return count == idList.size();}
}
